import bodyParser from 'body-parser';
import session from 'express-session';
const LRU = require('lru-cache');
const fetch = require('node-fetch');
const nuxtPageCache = require('nuxt-page-cache');

export default {
  /*
  ** Single Page Application mode
  ** Means no SSR
  */
  // mode: 'spa',
  loading: {
    color: '#4FC08D',
    failedColor: '#bf5050',
    duration: 1500
  },
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'nuxt_demo',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { hid: 'defaultKeywords', name: 'keywords', content: 'nuxt,手机,数码,空调...' },
      { name: 'viewport', content: 'width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no,viewport-fit=cover' },
      { name: 'applicable-device', content: 'mobile' },
      { name: 'apple-mobile-web-app-capable', content: 'yes' },
      { name: 'format-detection', content: 'telephone=no' },
      { name: 'wap-font-scale', content: 'no' },
      { 'http-equiv': 'Expires', content: '-1' },
      { 'http-equiv': 'Cache-Control', content: 'no-cache, no-store, must-revalidate' },
      { 'http-equiv': 'Cache-Control', content: 'no-siteapp' },
      { 'http-equiv': 'Cache-Control', content: 'no-transform' },
      { 'http-equiv': 'Pragma', content: 'no-cache' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ],
    script: [
      { src: 'https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver', body: true }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: ['assets/common.css', 'assets/main.css'],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    '@/plugins/vue-placeholders.js',
    '~/plugins/i18n.js',
    '~/plugins/md-it',
    '~/plugins/cookies',
    // ssr: false to only include it on client-side
    { src: '~/plugins/vue-notifications.js', ssr: false },
    '~/plugins/filters.js',
    { src: '~/plugins/vue-lazyload', ssr: false },
    '~/plugins/ctx-inject.js',
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    '@nuxtjs/style-resources',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  // axios
  // modules 是 Nuxt.js 扩展 可以扩展它的核心功能并添加无限的集成
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/proxy',
    '@nuxt/http',
    '@nuxtjs/markdownit',
    '@nuxtjs/robots',
    '@nuxtjs/toast',
  ],

  toast: {// toast模块的配置
    position: 'top-center',
    duration: 2000
  },

  robots: {
    /* module options */
    UserAgent: '*',
    Disallow: '/'
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    vendor: ['axios', 'js-cookie'],
    // You cannot use ~/ or @/ here since it's a Webpack plugin
    styleResources: {
      // scss: './assets/variables.scss',
      less: './assets/*.less'
    }
  },
  // router 属性让你可以个性化配置 Nuxt.js 应用的路由
  router: {
    extendRoutes (routes, resolve) {
      const indexIndex = routes.findIndex(route => route.name === 'index')
      let index = routes[indexIndex].children.findIndex(route => route.name === 'index-child-id')
      routes[indexIndex].children[index] = {
        ...routes[indexIndex].children[index],
        components: {
          default: routes[indexIndex].children[index].component,
          left: resolve(__dirname, 'components/childLeft.vue')
        },
        chunkNames: {
          left: 'components/childLeft'
        }
      }

      index = routes.findIndex(route => route.name === 'main')
      routes[index] = {
        ...routes[index],
        components: {
          default: routes[index].component,
          top: resolve(__dirname, 'components/mainTop.vue')
        },
        chunkNames: {
          top: 'components/mainTop'
        }
      }
    },
    middleware: 'i18n',
    // To disable prefetching, uncomment the line
    // prefetchLinks: false

    // Activate prefetched class (default: false)
    // Used to display the check mark next to the prefetched link
    linkPrefetchedClass: 'nuxt-link-prefetched',
  },
  // 配置 Nuxt.js 应用生成静态站点的具体方式
  generate: {
    routes: [
      '/posts/1'
    ]
  },
  // Authenticated Routes
  /*
  ** Add server middleware
  ** Nuxt uses `connect` module as server
  ** So most of express middleware works with nuxt server middleware
  */
  serverMiddleware: [
    // body-parser middleware
    bodyParser.json(),
    // session middleware
    session({
      secret: 'super-secret-key',
      resave: false,
      saveUninitialized: false,
      cookie: { maxAge: 60000 }
    }),
    // Api middleware
    // We add /api/login & /api/logout routes
    '~/api',
    /**
     * 页面级别的缓存
     */
    nuxtPageCache.cacheSeconds(1, req => {
      if (req.query && req.query.pageType) {
        return req.query.pageType
      }
      return false
    })
  ],
  proxy: [
    [
      // https://dog.ceo/api/breeds/image/random
      '/dog',
      {
        target: 'https://dog.ceo/',
        pathRewrite: { '^/dog': '/api/breeds/image/random' }
      }
    ],[
      '/api',
      {
        target: 'https://banner-storage-ms.juejin.im',
        pathRewrite: { '^/api': '' }
      }
    ],[
      '/wapi',
      {
        target: 'https://web-api.juejin.im',
        pathRewrite: { '^/wapi': '' }
      }
    ]
  ],
  // cached-components
  // Nuxt.js 允许您自定义渲染页面的运行时选项
  // render: {
  //   bundleRenderer: {
  //     cache: require('lru-cache')({
  //       max: 1000,
  //       maxAge: 1000 * 60 * 15
  //     })
  //   }
  // },
  render: {
    bundleRenderer: {
      shouldPreload: (file, type) => {
        return ['script', 'style', 'font'].includes(type)
      },
      // 组件级别的缓存
      cache: LRU({
        max: 1000,                     // 最大的缓存个数
        maxAge: 1000 * 60 * 15        // 缓存15分钟
      })
    }
  },
  // Nuxt.js 让你可以配置在客户端和服务端共享的环境变量
  env: {
    NODE_ENV: process.env.NODE_ENV,
    users: [
      { id: 1, name: 'Kobe Bryant', number: 24 },
      { id: 2, name: 'Michael Jordan', number: 23 },
      { id: 3, name: 'Stephen Curry', number: 30 },
      { id: 4, name: 'Lebron James', number: 23 },
      { id: 5, name: 'Kevin Durant', number: 35 },
      { id: 6, name: 'Kyrie Irving', number: 2 }
    ],
  },
}
